package com.photoeditor.demo.ui.widget.image.edit;

import android.content.Context;
import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.graffiti.IndicativeHorizontalScrollView;
import com.photoeditor.R;
import com.photoeditor.demo.model.preference.SPDataManager;
import com.photoeditor.demo.util.FontUtil;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;


public class OtherBarView extends RelativeLayout implements View.OnClickListener {

    public static final int TYPE_OTHER_BAR = 4;

    @IntDef(value = {
            BarType.TYPE_ID_EFFECT,
            BarType.TYPE_ID_ADJUST,
            BarType.TYPE_ID_EDIT
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface BarType {
        int TYPE_ID_EFFECT = 1;
        int TYPE_ID_ADJUST = 2;
        int TYPE_ID_EDIT = 3;
    }

    @BarType
    private int mCurrentTypeId;

    private LinearLayout mEffectLayout;
    private LinearLayout mAdjustLayout;
    private LinearLayout mEditLayout;

    private IndicativeHorizontalScrollView mEffectScrollBar;
    private IndicativeHorizontalScrollView mAdjustScrollBar;
    private IndicativeHorizontalScrollView mEditScrollBar;

    private CustomTabButton mCropBt;

    private CustomTabButton mRotateBt;

	private CustomTabButton mAddStickerBt;

    private CustomTabButton mBeautyBt;

//    private CustomTabButton mDoodleBt;

    private CustomTabButton mTiltShiftBt;

    private CustomTabButton mAddTextBt;

    private CustomTabButton mWatermarkBt;

    private CustomTabButton mMirrorBt;

    private CustomTabButton mFilterBt;

    private CustomTabButton mAdjustBt;

    //    private CustomTabButton mEraseBt;
    private CustomTabButton mMixerBt;

    private View mBeautyRedDot;

    //底部按钮
    private TextView mEffectBottomBtn;
    private TextView mAdjustBottomBtn;
    private TextView mEditBottomBtn;

    private boolean mIsInflated = false;

    private float mHorizontalItemWidth = -1;
    private int mBarItemCount = -1;
    private float mFraction;
    private float mHorizontalBarScrollX;

    private Animation mAlphaIn;
    private Animation mAlphaOut;

    public OtherBarView(Context context) {
        this(context, null);
    }

    public OtherBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mEffectLayout = (LinearLayout) findViewById(R.id.effect_layout);
        mAdjustLayout = (LinearLayout) findViewById(R.id.adjust_layout);
        mEditLayout = (LinearLayout) findViewById(R.id.edit_layout);

        mEffectScrollBar = findViewById(R.id.effect_scroll_view);
        mAdjustScrollBar = findViewById(R.id.adjust_scroll_view);
        mEditScrollBar = findViewById(R.id.edit_scroll_view);

        mCropBt = (CustomTabButton) findViewById(R.id.crop);
        mRotateBt = (CustomTabButton) findViewById(R.id.rotate);
		mAddStickerBt = (CustomTabButton)findViewById(R.id.sticker);
        mBeautyBt = (CustomTabButton) findViewById(R.id.beauty);
//        mDoodleBt = (CustomTabButton) findViewById(R.id.doodle);
        mTiltShiftBt = (CustomTabButton) findViewById(R.id.tilt_shift);
        mAddTextBt = (CustomTabButton) findViewById(R.id.addtext);
        mWatermarkBt = (CustomTabButton) findViewById(R.id.watermark);
        mMirrorBt = (CustomTabButton) findViewById(R.id.mirror);
        mFilterBt = (CustomTabButton) findViewById(R.id.filter);
        mAdjustBt = (CustomTabButton) findViewById(R.id.adjust);
//        mEraseBt = (CustomTabButton) findViewById(R.id.op_eraser);
        mMixerBt = (CustomTabButton) findViewById(R.id.op_mixer);

        mEffectBottomBtn = (TextView) findViewById(R.id.btn_effect);
        mAdjustBottomBtn = (TextView) findViewById(R.id.btn_adjust);
        mEditBottomBtn = (TextView) findViewById(R.id.btn_edit);
        mEffectBottomBtn.setOnClickListener(this);//
        mAdjustBottomBtn.setOnClickListener(this);//
        mEditBottomBtn.setOnClickListener(this);//

        mBeautyRedDot = findViewById(R.id.beauty_reddot);

        mIsInflated = true;

        initView();

        mCurrentTypeId = BarType.TYPE_ID_EFFECT;
    }

    public void setCurrentType(@BarType int typeId) {
        if (mCurrentTypeId == typeId) {
            return;
        }
        switchBar(getTypeView(mCurrentTypeId), getTypeView(typeId));
        mCurrentTypeId = typeId;
    }

    public int getCurrentTypeId() {
        return mCurrentTypeId;
    }

    private IndicativeHorizontalScrollView getTypeView(@BarType int typeId) {
        switch (typeId) {
            case BarType.TYPE_ID_EFFECT:
                return mEffectScrollBar;
            case BarType.TYPE_ID_ADJUST:
                return mAdjustScrollBar;
            case BarType.TYPE_ID_EDIT:
                return mEditScrollBar;
            default:
                return null;
        }
    }

    @Override
    public void onClick(View v) {
        int viewId = v.getId();
        switch (viewId) {
            case R.id.btn_effect:
                setCurrentType(BarType.TYPE_ID_EFFECT);
                break;
            case R.id.btn_adjust:
                setCurrentType(BarType.TYPE_ID_ADJUST);
                break;
            case R.id.btn_edit:
                setCurrentType(BarType.TYPE_ID_EDIT);
                break;
            default:
                break;
        }
        setButtonTextFont(v);
    }

    //点击时改变字体粗体
    private void setButtonTextFont(View v) {
        switch (v.getId()) {
            case R.id.btn_effect:
                FontUtil.setCustomFontBold(mEffectBottomBtn);
                FontUtil.setCustomFont(mEditBottomBtn);
                FontUtil.setCustomFont(mAdjustBottomBtn);
                break;
            case R.id.btn_adjust:
                FontUtil.setCustomFont(mEffectBottomBtn);
                FontUtil.setCustomFont(mEditBottomBtn);
                FontUtil.setCustomFontBold(mAdjustBottomBtn);
                break;
            case R.id.btn_edit:
                FontUtil.setCustomFont(mEffectBottomBtn);
                FontUtil.setCustomFontBold(mEditBottomBtn);
                FontUtil.setCustomFont(mAdjustBottomBtn);
                break;
            default:
                break;
        }
    }

    private void switchBar(View currentView, View nextView) {
        if (currentView != null) {
            currentView.setVisibility(View.GONE);
            currentView.startAnimation(getAlphaOut());
        }
        if (nextView != null) {
            nextView.setVisibility(VISIBLE);
            nextView.startAnimation(getAlphaIn());
        }
    }


    public Animation getAlphaIn() {
        if (mAlphaIn == null) {
            mAlphaIn = new AlphaAnimation(0f, 1f);
            mAlphaIn.setDuration(180);
        } else {
            mAlphaIn.reset();
        }
        return mAlphaIn;
    }

    public Animation getAlphaOut() {
        if (mAlphaOut == null) {
            mAlphaOut = new AlphaAnimation(1f, 0f);
            mAlphaOut.setDuration(180);
        } else {
            mAlphaOut.reset();
        }
        return mAlphaOut;
    }

    @Override
    protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
        super.onVisibilityChanged(changedView, visibility);
        if (visibility == VISIBLE && mBeautyRedDot != null) {
            if (SPDataManager.hasUseBoobJob() && SPDataManager.hasUseButtocks()) {
                mBeautyRedDot.setVisibility(GONE);
            }
        }
    }

    /**
     * 设置OnClickListener监听
     */
    @Override
    public void setOnClickListener(OnClickListener listener) {
        if (mIsInflated) {
            mCropBt.setOnClickListener(listener);
            mRotateBt.setOnClickListener(listener);
			mAddStickerBt.setOnClickListener(listener);
//            mDoodleBt.setOnClickListener(listener);
            mTiltShiftBt.setOnClickListener(listener);
            mAddTextBt.setOnClickListener(listener);
            mWatermarkBt.setOnClickListener(listener);
            mBeautyBt.setOnClickListener(listener);
            mMirrorBt.setOnClickListener(listener);
            mFilterBt.setOnClickListener(listener);
            mAdjustBt.setOnClickListener(listener);
//            mEraseBt.setOnClickListener(listener);
            mMixerBt.setOnClickListener(listener);
        }
    }

    public void initView() {
        int textColor = getResources().getColor(R.color.image_edit_sencond_text_color);

        mCropBt.setThemeImageRes(R.drawable.ic_tool_crop, View.NO_ID);
        mRotateBt.setThemeImageRes(R.drawable.ic_tool_rotate, View.NO_ID);
        mBeautyBt.setThemeImageRes(R.drawable.ic_tool_beauty, View.NO_ID);
//        mDoodleBt.setThemeImageRes(R.drawable.ic_tool_doodle, View.NO_ID);
        mTiltShiftBt.setThemeImageRes(R.drawable.ic_tool_blur, View.NO_ID);
        mAddTextBt.setThemeImageRes(R.drawable.ic_tool_text, View.NO_ID);
        mWatermarkBt.setThemeImageRes(R.drawable.ic_tool_watermark, View.NO_ID);
        mMirrorBt.setThemeImageRes(R.drawable.ic_tool_mirror, View.NO_ID);
        mFilterBt.setThemeImageRes(R.drawable.ic_edit_filter, View.NO_ID);
        mAdjustBt.setThemeImageRes(R.drawable.ic_edit_adjust, View.NO_ID);
//        mEraseBt.setThemeImageRes(R.drawable.ic_edit_eraser, View.NO_ID);
        mMixerBt.setThemeImageRes(R.drawable.ic_edit_mixer, View.NO_ID);
        mAddStickerBt.setThemeImageRes(R.drawable.ic_edit_sticker, View.NO_ID);

        //设置默认文字粗体
        mEffectScrollBar.post(new Runnable() {
            @Override
            public void run() {
                if (mEffectScrollBar.getVisibility() == View.VISIBLE)
                    FontUtil.setCustomFontBold(mEffectBottomBtn);
                else if (mEditScrollBar.getVisibility() == View.VISIBLE)
                    FontUtil.setCustomFontBold(mEditBottomBtn);
                else if (mAdjustScrollBar.getVisibility() == View.VISIBLE)
                    FontUtil.setCustomFontBold(mAdjustBottomBtn);
            }
        });
    }

    public void scrollToStart() {
//        if (mScrollBar != null) {
//            mScrollBar.scrollTo(0, 0);
//        }
//        mHorizontalBarScrollX = 0;
    }

    public void getItemWidth() {
//		if (mCropBt.getVisibility() == VISIBLE) {
//			mHorizontalItemWidth = mCropBt.getMeasuredWidth();
//		}
    }

    public void getBarItemCount() {
//		if (mHorizontalItemWidth <= 0) {
//			mBarItemCount = -1;
//			return;
//		}
//		float countF = DeviceUtils.getScreenWidthPx() * 1f / mHorizontalItemWidth;
//		mBarItemCount = (int) (DeviceUtils.getScreenWidthPx() * 1f / mHorizontalItemWidth);
//		mFraction = countF - mBarItemCount;
    }

    public void statisticItemShow(float scrollX) {
//		if (mHorizontalItemWidth <=0 || mBarItemCount <=0) {
//			return;
//		}
//		float scrollCountF = scrollX / mHorizontalItemWidth;
//		if ((int)scrollCountF != 0) {
//			scrollCountF += mFraction;
//		}
//		int tmpTotalCount = 0;
//		int scrollCountInt = (int) scrollCountF;
//		if (scrollCountF - scrollCountInt > 0.5f) {
//			if (scrollCountF - scrollCountInt > 0.6f) {
//				tmpTotalCount = mBarItemCount + 1;
//			} else {
//				tmpTotalCount = mBarItemCount;
//			}
//			scrollCountInt++;
//		} else if (scrollCountF - scrollCountInt < 0.05f && scrollCountF - scrollCountInt > 0){
//			tmpTotalCount = mBarItemCount + 1;
//		} else {
//			tmpTotalCount = mBarItemCount;
//		}
//		int lastIndex = mBarItemCount + scrollCountInt - 1;
//		if (lastIndex >= mMainLayout.getChildCount()) {
//			lastIndex = mMainLayout.getChildCount() - 1;
//		}
//		int startIndex = lastIndex - tmpTotalCount + 1;
//		if (startIndex < 0 ) {
//			startIndex = 0;
//		}
//		for (int i = startIndex; i <= lastIndex; i++) {
//			statisticEditItemShow(i);
//		}
    }

    private void statisticEditItemShow(int index) {
//		switch (index) {
//			case 0:
//				StatisticsUtils.statisicsCustomFunction(UMENG_CUSTOM_EVENT_EDITOR, UMENG_CUSTOM_KEY_FUNCTION,
//						MAIN_BTN_EDIT + UMENG_CUSTOM_UNDER_LINE + "stickers" + UMENG_CUSTOM_EVENT_FUNC_SHOW);
//				break;
//			case 1:
//				StatisticsUtils.statisicsCustomFunction(UMENG_CUSTOM_EVENT_EDITOR, UMENG_CUSTOM_KEY_FUNCTION,
//						MAIN_BTN_EDIT + UMENG_CUSTOM_UNDER_LINE + "filter" + UMENG_CUSTOM_EVENT_FUNC_SHOW);
//				break;
//			case 2:
//				StatisticsUtils.statisicsCustomFunction(UMENG_CUSTOM_EVENT_EDITOR, UMENG_CUSTOM_KEY_FUNCTION,
//						MAIN_BTN_EDIT + UMENG_CUSTOM_UNDER_LINE + "crop" + UMENG_CUSTOM_EVENT_FUNC_SHOW);
//				break;
//			case 3:
//				StatisticsUtils.statisicsCustomFunction(UMENG_CUSTOM_EVENT_EDITOR, UMENG_CUSTOM_KEY_FUNCTION,
//						MAIN_BTN_EDIT + UMENG_CUSTOM_UNDER_LINE + "adjust" + UMENG_CUSTOM_EVENT_FUNC_SHOW);
//				break;
//			case 4:
//				StatisticsUtils.statisicsCustomFunction(UMENG_CUSTOM_EVENT_EDITOR, UMENG_CUSTOM_KEY_FUNCTION,
//						MAIN_BTN_EDIT + UMENG_CUSTOM_UNDER_LINE + "watermark" + UMENG_CUSTOM_EVENT_FUNC_SHOW);
//				break;
//			case 5:
//				StatisticsUtils.statisicsCustomFunction(UMENG_CUSTOM_EVENT_EDITOR, UMENG_CUSTOM_KEY_FUNCTION,
//						MAIN_BTN_EDIT + UMENG_CUSTOM_UNDER_LINE + "text" + UMENG_CUSTOM_EVENT_FUNC_SHOW);
//				break;
//			case 6:
//				StatisticsUtils.statisicsCustomFunction(UMENG_CUSTOM_EVENT_EDITOR, UMENG_CUSTOM_KEY_FUNCTION,
//						MAIN_BTN_EDIT + UMENG_CUSTOM_UNDER_LINE + "doodle" + UMENG_CUSTOM_EVENT_FUNC_SHOW);
//				break;
//			case 7:
//				StatisticsUtils.statisicsCustomFunction(UMENG_CUSTOM_EVENT_EDITOR, UMENG_CUSTOM_KEY_FUNCTION,
//						MAIN_BTN_EDIT + UMENG_CUSTOM_UNDER_LINE + "mixer" + UMENG_CUSTOM_EVENT_FUNC_SHOW);
//				break;
//			case 8:
//				StatisticsUtils.statisicsCustomFunction(UMENG_CUSTOM_EVENT_EDITOR, UMENG_CUSTOM_KEY_FUNCTION,
//						MAIN_BTN_EDIT + UMENG_CUSTOM_UNDER_LINE + "mirror" + UMENG_CUSTOM_EVENT_FUNC_SHOW);
//				break;
//			case 9:
//				StatisticsUtils.statisicsCustomFunction(UMENG_CUSTOM_EVENT_EDITOR, UMENG_CUSTOM_KEY_FUNCTION,
//						MAIN_BTN_EDIT + UMENG_CUSTOM_UNDER_LINE + "blur" + UMENG_CUSTOM_EVENT_FUNC_SHOW);
//				break;
//			case 10:
//				StatisticsUtils.statisicsCustomFunction(UMENG_CUSTOM_EVENT_EDITOR, UMENG_CUSTOM_KEY_FUNCTION,
//						MAIN_BTN_EDIT + UMENG_CUSTOM_UNDER_LINE + "rotate" + UMENG_CUSTOM_EVENT_FUNC_SHOW);
//				break;
//			default:
//				break;
//		}
    }

    public void statisticEditItemClick(int id) {
//		switch (id) {
//			case R.id.addemoji:
//				StatisticsUtils.statisicsCustomFunction(UMENG_CUSTOM_EVENT_EDITOR, UMENG_CUSTOM_KEY_FUNCTION,
//						MAIN_BTN_EDIT + UMENG_CUSTOM_UNDER_LINE + "stickers" + UMENG_CUSTOM_EVENT_FUNC_CLICK);
//				SPDataManager.setSaveFunctionName("stickers");
//				break;
//			case R.id.filter:
//				StatisticsUtils.statisicsCustomFunction(UMENG_CUSTOM_EVENT_EDITOR, UMENG_CUSTOM_KEY_FUNCTION,
//						MAIN_BTN_EDIT + UMENG_CUSTOM_UNDER_LINE + "filter" + UMENG_CUSTOM_EVENT_FUNC_CLICK);
//				SPDataManager.setSaveFunctionName("filter");
//				break;
//			case R.id.crop:
//				StatisticsUtils.statisicsCustomFunction(UMENG_CUSTOM_EVENT_EDITOR, UMENG_CUSTOM_KEY_FUNCTION,
//						MAIN_BTN_EDIT + UMENG_CUSTOM_UNDER_LINE + "crop" + UMENG_CUSTOM_EVENT_FUNC_CLICK);
//				SPDataManager.setSaveFunctionName("crop");
//				break;
//			case R.id.adjust:
//				StatisticsUtils.statisicsCustomFunction(UMENG_CUSTOM_EVENT_EDITOR, UMENG_CUSTOM_KEY_FUNCTION,
//						MAIN_BTN_EDIT + UMENG_CUSTOM_UNDER_LINE + "adjust" + UMENG_CUSTOM_EVENT_FUNC_CLICK);
//				SPDataManager.setSaveFunctionName("adjust");
//				break;
//			case R.id.watermark:
//				StatisticsUtils.statisicsCustomFunction(UMENG_CUSTOM_EVENT_EDITOR, UMENG_CUSTOM_KEY_FUNCTION,
//						MAIN_BTN_EDIT + UMENG_CUSTOM_UNDER_LINE + "watermark" + UMENG_CUSTOM_EVENT_FUNC_CLICK);
//				SPDataManager.setSaveFunctionName("watermark");
//				break;
//			case R.id.addtext:
//				StatisticsUtils.statisicsCustomFunction(UMENG_CUSTOM_EVENT_EDITOR, UMENG_CUSTOM_KEY_FUNCTION,
//						MAIN_BTN_EDIT + UMENG_CUSTOM_UNDER_LINE + "text" + UMENG_CUSTOM_EVENT_FUNC_CLICK);
//				SPDataManager.setSaveFunctionName("text");
//				break;
//			case R.id.doodle:
//				StatisticsUtils.statisicsCustomFunction(UMENG_CUSTOM_EVENT_EDITOR, UMENG_CUSTOM_KEY_FUNCTION,
//						MAIN_BTN_EDIT + UMENG_CUSTOM_UNDER_LINE + "doodle" + UMENG_CUSTOM_EVENT_FUNC_CLICK);
//				SPDataManager.setSaveFunctionName("doodle");
//				break;
//			case R.id.op_mixer:
//				StatisticsUtils.statisicsCustomFunction(UMENG_CUSTOM_EVENT_EDITOR, UMENG_CUSTOM_KEY_FUNCTION,
//						MAIN_BTN_EDIT + UMENG_CUSTOM_UNDER_LINE + "mixer" + UMENG_CUSTOM_EVENT_FUNC_CLICK);
//				SPDataManager.setSaveFunctionName("mixer");
//				break;
//			case R.id.mirror:
//				StatisticsUtils.statisicsCustomFunction(UMENG_CUSTOM_EVENT_EDITOR, UMENG_CUSTOM_KEY_FUNCTION,
//						MAIN_BTN_EDIT + UMENG_CUSTOM_UNDER_LINE + "mirror" + UMENG_CUSTOM_EVENT_FUNC_CLICK);
//				SPDataManager.setSaveFunctionName("mirror");
//				break;
//			case R.id.tilt_shift:
//				StatisticsUtils.statisicsCustomFunction(UMENG_CUSTOM_EVENT_EDITOR, UMENG_CUSTOM_KEY_FUNCTION,
//						MAIN_BTN_EDIT + UMENG_CUSTOM_UNDER_LINE + "blur" + UMENG_CUSTOM_EVENT_FUNC_CLICK);
//				SPDataManager.setSaveFunctionName("blur");
//				break;
//			case R.id.rotate:
//				StatisticsUtils.statisicsCustomFunction(UMENG_CUSTOM_EVENT_EDITOR, UMENG_CUSTOM_KEY_FUNCTION,
//						MAIN_BTN_EDIT + UMENG_CUSTOM_UNDER_LINE + "rotate" + UMENG_CUSTOM_EVENT_FUNC_CLICK);
//				SPDataManager.setSaveFunctionName("rotate");
//				break;
//			default:
//				break;
//		}
    }

    public float getHorizontalBarScrollX() {
        return mHorizontalBarScrollX;
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
//        if (mScrollBar != null) {
//            mScrollBar.cancelOtherBarViewStatisticTask();
//        }
    }

}
